package NIO;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

public class WriteClient {
    public static void main(String[] args) throws IOException {
        Selector selector= Selector.open();
        SocketChannel sc=SocketChannel.open();
        sc.configureBlocking(false);
        sc.connect(new InetSocketAddress("localhost",8080));
        sc.register(selector, SelectionKey.OP_CONNECT+SelectionKey.OP_READ, null);
        int count=0;
        while (true){
            selector.select();
            Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
            while (iter.hasNext()){
                SelectionKey key = iter.next();
                iter.remove();
                if (key.isConnectable()){
                    System.out.println(sc.finishConnect());
                }else if (key.isReadable()){
                    ByteBuffer byteBuffer=ByteBuffer.allocate(1024*1024);
                    count+=sc.read(byteBuffer);
                    byteBuffer.clear();
                    System.out.println(count);
                }
            }
        }

    }
}
